<?php

namespace App\Library\Service;

use App\Library\Logic\DemoLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Demo\DemoAddRequest;
use App\Library\Requests\Demo\DemoEditRequest;
use App\Library\Requests\Demo\DemoPageRequest;
use Illuminate\Http\JsonResponse;

class DemoService extends BaseService
{
    /**
     * @param DemoLogic $logic
     */
    public function __construct(DemoLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * paging
     * @param DemoPageRequest $request
     * @return JsonResponse
     */
    public function paging(DemoPageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * add
     * @param DemoAddRequest $request
     * @return JsonResponse
     */
    public function add(DemoAddRequest $request): JsonResponse
    {
        $this->logic->insertForModel(handleRequest($request));
        return success();
    }

    /**
     * edit
     * @param DemoEditRequest $request
     * @return JsonResponse
     */
    public function edit(DemoEditRequest $request): JsonResponse
    {
        $item = $this->logic->findBySerialNumber($request);
        if (is_null($item)) return fail(433);
        $this->logic->updateForModel(handleRequest($request), $item);
        return success();
    }

    /**
     * detail
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function detail(SerialNumberRequest $request): JsonResponse
    {
        $item = $this->logic->findBySerialNumber($request);
        if (is_null($item)) return fail(433);
        return success($item);
    }

    /**
     * delete
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function delete(SerialNumberRequest $request): JsonResponse
    {
        $item = $this->logic->findBySerialNumber($request);
        if (is_null($item)) return fail(433);
        else $item->delete();
        return success();
    }
}
